<template>
  <div v-if="arr">
    <div class="cart" v-show="!token">
      <div class="head">
        <h1>购物车</h1>
      </div>
      <div class="content">
        <div class="img">
          <span></span>
        </div>
        <h3>购物车暂无商品</h3>
        <p>添加到购物车的商品将会显示在这里</p>
        <div class="bts">
          <div class="left">
            <router-link tag="span" to="/login">登录</router-link>
          </div>
          <div class="right" @click="buy">
            <span>现在购买</span>
          </div>
        </div>
      </div>
      <buttomtag></buttomtag>
    </div>
    <div class="cart" v-show="token">
      <div class="head">
        <h1>购物车</h1>
      </div>
      <div class="content" v-show="cartshow.length == 0">
        <div class="img">
          <span></span>
        </div>
        <h3>购物车暂无商品</h3>
        <p>添加到购物车的商品将会显示在这里</p>
        <div class="bts">
          <div class="right" @click="buy">
            <span class="have">现在购买</span>
          </div>
        </div>
      </div>
      <div class="middle" v-show="cartshow.length != 0">
        <div class="content" v-for="(item,index) in cartshow" :key="index">
          <div class="check" @click="chance(item.id)">
            <img src="../assets/img/22.png" alt="" v-show="item.list.checked" />
            <img
              src="../assets/img/33.png"
              alt=""
              v-show="!item.list.checked"
            />
          </div>
          <div class="left">
            <img :src="item.list.url" alt="" />
          </div>
          <div class="right">
            <h5>{{ item.list.name }}</h5>
            <div class="add">
              <img src="../assets/img/sub.png" alt="" @click="sub(index)" />
              <p>{{ item.list.num }}</p>
              <img src="../assets/img/add.png" alt="" @click="add(index)" />
            </div>
            <span>&yen; {{ item.list.price }} </span>
          </div>
          <div class="del" @click="del(index)">删除</div>
        </div>
      </div>
      <div class="num" v-show="cartshow.length != 0">
        <div class="title">
          <div class="check" @click="allChance()">
          <h6>全部</h6>
            <img src="../assets/img/22.png" alt="" v-show="all" />
            <img src="../assets/img/33.png" alt="" v-show="!all" />
          </div>
          
          <h4>合计:</h4>
          <h2>&yen;{{ num }}.00</h2>
          <span @click="end">结算</span>
        </div>
      </div>
      <buttomtag></buttomtag>
    </div>
  </div>
</template>

<script>
import buttomtag from "../components/TestoCom.vue";

export default {
  props: ["arr"],
  data() {
    return {
      token: null,
      test: "",
      cartshow: null,
      flag: false,
      all: true,
      check: true,
    };
  },

  methods: {
    buy() {
      this.$router.push("/home");
      window.localStorage.setItem("btindex", 1);
    },
    end(){
       this.$router.push('/enter')
        var newarr=this.arr.filter(function(item){
          return item.list.checked==true
     })
    //  console.log('newarr=====',newarr);
      this.cartshow=this.cartshow.filter(item=>!item.list.checked)

      // window.localStorage.setItem("cartshow",JSON.stringify(this.cartshow))
      console.log(this.cartshow);
      this.$emit('updata', {list1:newarr,list2:this.cartshow});
       
    },
    //点击删除
    del(i) {
      
      this.cartshow.splice(i, 1);
    },
    add(i) {
     
      this.cartshow[i].list.num += 1;
    },
    sub(i) {
      
      if (this.cartshow[i].list.num <= 1) {
        return;
      } else {
        this.cartshow[i].list.num -= 1;
      }
    },
    chance(i) {
      let index=this.cartshow.findIndex(function (item) {
          return item.id ==i
      })
      console.log(this.cartshow[index]);
      this.cartshow[index].list.checked = !this.cartshow[index].list.checked;
      this.all = this.cartshow.every(function (item) {
        return item.list.checked == true;
      });
    },
    allChance() {
      this.all = !this.all;
      if (this.all == true) {
        this.cartshow.forEach((item) => {
          item.list.checked = true;
        });
      }else{
         this.cartshow.forEach((item) => {
          item.list.checked = false;
        })
      }
    },
  },
  components: {
    buttomtag,
  },
  created() {
    // 底部栏下标
    this.test = window.localStorage.getItem("token");
    this.cartshow = this.arr;
    // let temp = JSON.parse(window.localStorage.getItem("cartshow"))
    // this.cartshow=temp==null? this.arr:temp
    console.log(this.cartshow);
   
  },
  computed: {
    num() {
      return this.cartshow.reduce(function (total, item) {
        if (item.list.checked == true) {
          return (total += Number(item.list.price * item.list.num));
        }
        return total;
      }, 0);
    },
  },

  watch: {
    test: function () {
      if (this.test) {
        this.token = this.test;
      } else {
        this.token = null;
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.cart {
  .head {
    width: 100%;
    height: 50px;
    border-bottom: 4px solid #f6f4f4;
    position: fixed;
    top: 0px;
    z-index: 55;
    background-color: #fff;
    h1 {
      width: 70%;
      margin: 0 auto;
      text-align: center;
      line-height: 50px;
      color: rgba(0, 0, 0, 0.6);
      font-size: 18px;
      font-weight: 700;
    }
  }
  .content {
    width: 100%;

    .img {
      width: 40%;
      margin: 0 auto;
      margin-top: 120px;
      height: 200px;
      // background-color: aqua;
      span {
        display: block;
        width: 100%;
        height: 190px;
        background-image: url(../assets/img/gwucart.png);
        background-repeat: no-repeat;
        background-size: cover;
      }
    }
    h3 {
      width: 50%;
      color: rgba(0, 0, 0, 0.15);
      font-weight: 700;
      margin: 0 auto;
      text-align: center;
    }
    p {
      width: 70%;
      font-size: 10px;
      margin: 0 auto;
      text-align: center;
      color: rgba(0, 0, 0, 0.15);
    }
    .bts {
      margin: 0 auto;
      margin-top: 20px;
      width: 80%;
      display: flex;
      //   justify-content: space-between;
      justify-content: center;
      .left {
        width: 20%;
        padding: 10px 40px;
        background: linear-gradient(#6f94e8, #5178df);
        border-radius: 6px;
        span {
          display: block;
          width: 100%;
          height: 20px;
          text-align: center;
          color: #fff;
          font-size: 12px;
          line-height: 20px;
        }
      }
      .right {
        width: 20%;
        padding: 10px 40px;
        border: 1px solid rgba(27, 26, 26, 0.12);
        border-radius: 6px;
        span {
          display: block;
          width: 100%;
          height: 20px;
          font-size: 12px;
          line-height: 20px;
          font-weight: 700;
          color: #666;
          text-align: center;
        }
      }
    }
  }
  .middle {
    margin-top: 50px;
    .content {
      width: 95%;
      height: 130px;
      margin-top: 20px;
      background-color: #fff;
      display: flex;
      justify-content: flex-start;
      border-bottom: 1px solid #dfdede;
      .check {
        display: flex;
        flex-direction: column;
        justify-content: center;
        margin-left: 10px;
        position: relative;
        img {
          display: block;
          width: 20px;
          height: 20px;
        }
       
      }
      .left {
        width: 40%;

        img {
          display: block;

          width: 120px;
          height: 120px;
        }
      }
      .right {
        width: 45%;
        display: flex;
        flex-direction: column;
        justify-content: center;

        h5 {
          overflow: hidden;
          white-space: nowrap;
          text-overflow: ellipsis;
          color: #333;
          font-weight: 700;
          padding-bottom: 8px;
        }
        .add {
          display: flex;
          width: 80px;
          margin: 10px 0;
          p {
            width: 10px;
            height: 20px;
            // border: 1px solid gray;

            color: #292828;
            line-height: 20px;
            text-align: center;
          }
          img {
            display: block;
            width: 20px;
            height: 20px;
          }
        }
        span {
          color: #d44d44;
          font-weight: 700;
          font-size: 14px;
        }
      }
      .del {
        width: 15%;
        height: 35px;
        line-height: 35px;
        border: 1px solid #c6c1c1;
        margin-top: 18%;
        background-color: #e73e3e;
        font-size: 14px;
        text-align: center;
        color: #f7f4f4;
        border-radius: 6px;
      }
    }
  }
  .num {
    width: 100%;
    height: 50px;
    position: fixed;
    bottom: 56px;
    z-index: 50;
    background-color: #ffffff;
    .title {
      display: flex;

      padding: 5px 0;
      .check {
        display: flex;
        flex-direction: column;
        justify-content: center;
        position: relative;
        img {
          display: block;
          width: 20px;
          height: 20px;
          margin-left: 10px;
          margin-right: 100px;
        }
         h6{
          font-size: 12px;
         position: absolute;
         left: 35px;
        }
      }
      h4 {
        height: 30px;
        font-size: 13px;
        line-height: 30px;
        padding-right: 5px;
        width: 40px;
      }
      h2 {
        color: red;
        height: 30px;
        line-height: 30px;
        width: 100px;
      }
      span {
        width: 50px;
        display: block;
        height: 30px;
        line-height: 30px;
        padding: 0px 20px;
        background-color: #fd5d00;
        border-radius: 20px;
        color: white;
        margin-right: 20px;
        margin-left: 5px;
        text-align: center;
      }
    }
  }
}
</style>